package com.lonely.fx.tree_table_view;

import com.lonely.fx.tree_table_view.entity.Person;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @date: 2022-08-04 16:53
 * @author: ztkj-hzb
 * @description: treeTableView 基础属性设置
 **/
public class TreeTableViewWithBaseSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        TreeTableView<Person> treeTableView = new TreeTableView<>();

        //创建tree数据
        TreeItem<Person> root = new TreeItem<>(new Person("汇总", null, null, null));

        TreeItem<Person> treeItem_1 = new TreeItem<>(new Person("小天", 22, 0.25, true));
        TreeItem<Person> treeItem_2 = new TreeItem<>(new Person("小地", 24, 0.5, false));
        TreeItem<Person> treeItem_3 = new TreeItem<>(new Person("小玄", 26, 0.75, true));
        TreeItem<Person> treeItem_4 = new TreeItem<>(new Person("小黄", 28, 1.0, false));
        TreeItem<Person> treeItem_5 = new TreeItem<>(new Person("小宇", 30, 0.25, true));
        TreeItem<Person> treeItem_6 = new TreeItem<>(new Person("小宙", 32, 0.50, false));
        TreeItem<Person> treeItem_7 = new TreeItem<>(new Person("小洪", 34, 0.75, true));
        TreeItem<Person> treeItem_8 = new TreeItem<>(new Person("小荒", 36, 1.0, false));

        root.getChildren().addAll(treeItem_1, treeItem_2, treeItem_3, treeItem_4);
        treeItem_4.getChildren().addAll(treeItem_5, treeItem_6, treeItem_7, treeItem_8);


        //创建列数据
        TreeTableColumn<Person, String> nameCol = new TreeTableColumn<>("用户名");
        TreeTableColumn<Person, Integer> ageCol = new TreeTableColumn<>("年龄");
        TreeTableColumn<Person, Double> progressCol = new TreeTableColumn<>("学习进度");
        TreeTableColumn<Person, Boolean> passCol = new TreeTableColumn<>("是否合格");
        treeTableView.getColumns().addAll(nameCol, ageCol, progressCol, passCol);


        //数据绑定
        nameCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, String>("name"));
        ageCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, Integer>("age"));
        progressCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, Double>("progress"));
        passCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, Boolean>("pass"));


        //设置root节点
        treeTableView.setRoot(root);

        //是否显示root节点
        treeTableView.setShowRoot(false);

        //设置宽高
        treeTableView.setPrefSize(600, 400);

        //设置列宽自动均分
        treeTableView.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);

        //设置单元格大小
        treeTableView.setFixedCellSize(30);

        //设置显示动态菜单栏按钮
        treeTableView.setTableMenuButtonVisible(true);


        //设置可编辑
        treeTableView.setEditable(true);
        //设置名称编辑方式
        nameCol.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn());

        anchorPane.getChildren().addAll(treeTableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();

    }
}
